import {
	funcVerify,
} from '@utils/router'
import {
	funcModuleApi,
	funcDelApi,
} from '@utils/apis'
import {
	$U_GetCacheItem,
} from '@utils/storage'

let _submitUrl = ''
let _type = ''

module.exports = {

	add(comps, user, func_id, type) {
		if (!funcVerify(user, func_id))
			return false
		// comps[`${type}Form`].form('clear')
		// comps[`${type}Dlg`].dialog('open')
		_type = type
		if (!document.getElementById('func-edit-node')) {
			comps.pNode.append('<input id="func-edit-node" name="p_id" style="width:200px;">')
			comps.editNode = $("#func-edit-node")
		}
		comps.editId.textbox('readonly', false)
		if (type == 'module') {
			comps.editNode.combobox({
				label: '所属节点:',
				required: true,
				valueField: 'id',
				textField: 'name',
			})
		}
		if (type == 'func') {
			comps.editNode.combobox({
				label: '所属模块:',
				required: true,
				valueField: 'id',
				textField: 'name',
			})
		}
		comps.editDlg.form('clear')
		funcModuleApi().then(res => {
			let {
				nodeData,
				moduleData,
			} = res.result
			let data = []
			if (type == 'module')
				data = nodeData
			if (type == 'func')
				data = moduleData
			comps.editNode.combobox('loadData', data)
			comps.editDlg.dialog('open')
		})
		_submitUrl = user.funcMap[func_id].func_url
	},

	funcEdit(comps, user, func_id) {
		if (!funcVerify(user, func_id))
			return false
		let selected = comps.dataGrid.treegrid('getSelected')
		console.log('---selected---', selected)
		if (!selected) {
			$.messager.alert('警告', '请选择！');
			return false
		}
		if (selected.id == 'root') {
			$.messager.alert('警告', '根节点不能修改！');
			return false
		}
		if (document.getElementById('func-edit-node'))
			comps.editNode.combobox('destroy')
		comps.editId.textbox('readonly', true)
		let id = ''
		let url = ''
		let name = selected.module_name
		if (name) {
			_type = 'module'
			id = selected.module_id
			url = selected.route || ''
		} else {
			_type = 'func'
			id = selected.func_id
			name = selected.func_name
			url = selected.func_url
		}
		comps.editForm.form('load', {
			id,
			name,
			url,
			desc: selected.desc,
		})
		comps.editDlg.dialog('open')
		_submitUrl = user.funcMap[func_id].func_url
	},

	submit(comps, user, type) {
		$.messager.progress();
		let formComp = comps[`${type}Form`]
		let dlgComp = comps[`${type}Dlg`]
		console.log('---_submitUrl---', _submitUrl)
		formComp.form('submit', {
			url: _submitUrl,
			onSubmit: function(params) {
				params.type = _type
				let isValid = formComp.form('validate');
				if (!isValid) {
					$.messager.progress('close');
				}
				_submitUrl = ''
				return isValid;
			},
			success: function(res) {
				try {
					res = JSON.parse(res)
				} catch (e) {

				}
				if (res.code != '000') {
					$.messager.alert(res.code, res.message)
				}
				comps.dataGrid.treegrid('reload');
				$.messager.progress('close');
				dlgComp.dialog('close');
				formComp.form('clear')
			}
		})
	},

	funcDel(comps, user) {
		let selected = comps.dataGrid.treegrid('getSelected')
		console.log('---selected---', selected)
		if (!selected) {
			$.messager.alert('警告', '请选择！');
			return false
		}
		let id = ''
		let type = ''
		if (selected.module_name) {
			id = selected.module_id
			type = 'module'
		} else {
			id = selected.func_id
			type = 'func'
		}
		$.messager.confirm({
			title: '删除',
			msg: '确认删除？',
			fn: function(r) {
				if (r) {
					funcDelApi({
						id,
						type,
					}).then(res => {
						if (res.code != '000')
							$.messager.alert(res.code, res.message)
						else
							comps.dataGrid.treegrid('reload');
					})
				}
			}
		});
	},

	cancel(dlg) {
		dlg.dialog('close')
	},
}

function parseTree(list, moduleData, funcData) {
	for (let i = 0; i < list.length; i++) {
		let data = list[i]
		if (data.func_id && data.func_name) {
			funcData.push({
				id: data.func_id,
				text: data.func_name,
			})
		} else if (data.module_id && data.module_name) {
			moduleData.push({
				id: data.module_id,
				text: data.module_name,
			})
		}
		if (Array.isArray(data.children)) {
			parseTree(data.children, moduleData, funcData)
		}
	}
}